var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxDesignUserPcModel = function (obj) {
    YjxBaseModel.call(this,obj);

    // `pc_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
    // `doc_id` bigint(20) unsigned NOT NULL COMMENT '文档ID',
    // `page_id` bigint(20) unsigned NOT NULL COMMENT '文档页ID',
    // `design_pc_id` bigint(20) unsigned NOT NULL COMMENT '设计模版pc_id',
    // `pc_index` int(3) NOT NULL DEFAULT '0' COMMENT '下标（0开始）',
    // `backup_flg` int(1) NOT NULL DEFAULT '0' COMMENT '0普通页; 1保留页',
    // `data` mediumtext COMMENT '模版页内容',
    // `replaces` mediumtext COMMENT '可替换组',
    // `hash_tag` varchar(100) DEFAULT '' COMMENT '标记',
    // `thumb_s` text COMMENT '缩略图小',
    // `thumb_m` text COMMENT '缩略图中',
    // `thumb_l` text COMMENT '缩略图大',
    // `audio` text COMMENT '音频url数组',
    // `video` text COMMENT '视频url数组',
    // `media` text COMMENT '原始视频',
    // `dirty` int(1) NOT NULL DEFAULT '0' COMMENT '0未修改; 1页被修改过',
    // `create_time` datetime NOT NULL,
    // `update_time` datetime NOT NULL,
    // `update_by` varchar(50) NOT NULL DEFAULT '',
    // `del_flg` int(1) NOT NULL DEFAULT '0',
    // PRIMARY KEY (`pc_id`),
    // KEY `IDX_ITB_TMPL_PAGE_1` (`page_id`,`del_flg`),
    // KEY `IDX_ITB_TMPL_PAGE_2` (`page_id`,`del_flg`,`pc_index`),
    // KEY `IDX_ITB_TMPL_PAGE_DEL_FLG` (`del_flg`),
    // KEY `IDX_TG_USER_PC_1` (`doc_id`,`del_flg`)

};

YjxDesignUserPcModel.prototype = new YjxBaseModel();

// 取得表名
YjxDesignUserPcModel.prototype.getTableName = function(){
    return 'yjx_design_user_pc';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxDesignUserPcModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'pc_id='+this.pc_id;
};

/**
 * 根据doc_id/page_id/pc_id逻辑删除对应pc
 *
 * @author 王俊
 * @date 2017-11-19
 *
 * @param callback 回调
 *          err: 删除失败
 * 注意:
 * 1. 拷贝自  TgUserPcModel.prototype.delPcLogic
 * 2. 调用此方法的模型需存在以下属性(doc_id, page_id, pc_id 至少存在一个)
 *     doc_id: 作品唯一标识
 *     page_id: 页唯一标识
 *     pc_id: 页内容唯一标识
 *     update_by: 更新者
 */
YjxDesignUserPcModel.prototype.delPcLogic = function(callback){
    var THIS = this;
    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = 'UPDATE yjx.yjx_design_user_pc '+
            ' SET del_flg=1, update_time=sysdate(), update_by= '+THIS.update_by+
            ' WHERE del_flg = 0 ';
        var config=false;
        if(THIS.doc_id) {
            sql += ' AND doc_id= '+ THIS.doc_id;
            config=true;
        }
        if(THIS.page_id) {
            sql += ' AND page_id= '+ THIS.page_id;
            config=true;
        }
        if(THIS.pc_id) {
            sql += ' AND pc_id= '+ THIS.pc_id;
            config=true;
        }
        if(!config){
            return callback("却少参数");
        }
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            callback(error);
        });
    });
};


/**
 * 按 pageIndex 获取页内容
 *
 * @author 王俊
 * @date 2017-11-20
 *
 * @param callback
 *          error：错误
 *          items：找到所有对象
 *
 * 注意:
 * 1. 复制自 TgUserPcModel.js ItbBaseModel.prototype.findFromDBOrderByPageIndex, 具体功能待测
 * 2. 调用此方法的数据模型需存在 doc_id 属性
 *
 */
YjxDesignUserPcModel.prototype.findFromDBOrderByPageIndex = function(callback){
    var THIS = this;

    // 从mysql中查询
    pool.pool.getConnection(function(err, connection) {
        var sql = 'select a.*,b.page_index from yjx.yjx_design_user_pc a, yjx.yjx_design_user_page b '+
            ' where a.doc_id = '+THIS.doc_id+
            ' and a.del_flg=0 '+
            ' and b.doc_id= '+THIS.doc_id+
            ' and b.del_flg=0 '+
            ' and a.page_id=b.page_id '+
            ' order by b.page_index asc, a.pc_index asc ';

        // Use the connection
        connection.query(sql, function(error, items) {
            // And done with the connection.
            connection.release();

            // 回调
            callback(error, items);
        });

    });

};

/**
 * 逻辑删除某文档所有页内容数据
 *
 * @author 王俊
 * @date 2017-11-20
 *
 * @param docId     文档作品 id
 * @param callback  回调
 *          err: 删除失败
 */
YjxDesignUserPcModel.prototype.deleteUserPcByDocId = function (docId, callback) {

    pool.pool.getConnection(function (getConErr, connection) {
        if(getConErr) {
            return callback(getConErr);
        }
        var sql =
            ' UPDATE yjx.yjx_design_user_pc ydup ' +
            ' SET ydup.del_flg=1 ' +
            ' WHERE ydup.doc_id= ' + docId +
            '   AND ydup.del_flg=0 ';
        connection.query(sql, function (queryErr, result) {
            connection.release();
            if(queryErr) {
                console.log('YjxDesignUserPcModel.js [deleteUserPcByDocId] err: ' + queryErr);
            }
            return callback(queryErr);
        })
    });
};


module.exports = YjxDesignUserPcModel;

